package com.bgrconsulting.stockorderentry.service.rest.impl;


import java.io.StringReader;

import javax.jdo.PersistenceManager;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;

import com.bgrconsulting.stockorderentry.service.PMF;
import com.bgrconsulting.stockorderentry.service.converter.AccountConverter;
import com.bgrconsulting.stockorderentry.service.dao.AccountResponseDao;
import com.bgrconsulting.stockorderentry.service.model.Account;
import com.bgrconsulting.stockorderentry.service.model.AccountBalance;
import com.bgrconsulting.stockorderentry.service.model.CashAccountBalance;
import com.bgrconsulting.stockorderentry.service.model.DTBalance;
import com.bgrconsulting.stockorderentry.service.model.MarginAccountBalance;
import com.bgrconsulting.stockorderentry.service.model.response.AccountResponse;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@Path("/accounts/rest/account/")
public class AccountResource {
	@Context
	UriInfo uriInfo;
	@Context
	Request request;
	String id;
	
	@GET
	 @Produces({"application/xml", "application/json"})
	 @Path("/{accountId}")  
	 public Account getAccount(@PathParam ("accountId") String accountId) {
		
		
		
		//TODO: Actual get a real account once I figure out the best storage.
		/*String id = "12345678J1";
		
		// Create 
		Account account = null;
		
		AccountBalance ab = new AccountBalance();
		ab.setCashAvailableForWithDrawal(0.00);
		ab.setNetAccountValue(-663.20);
		ab.setNetCash(740.450013);
		ab.setSweepDepositAmount(0.00);
		ab.setTotalLongValue(122.23);
		ab.setTotalSecurituesMktValue(-454.22);
		ab.setAccountId(id);
		
		
		DTBalance dtb = new DTBalance();
		dtb.setDTCashBalance(0.00);
		dtb.setDTMarginBalance(0.00);
		dtb.setDTMarginSecurities(-4183.79);
		dtb.setDTNonMarginableSecuritiesAndOptions(-2714.88);
		dtb.setDTStatus("QIALIFIED_4_X");
		dtb.setAccountId(id);
		
		MarginAccountBalance mab = new MarginAccountBalance();
		
		mab.setDTBalance(dtb);
		mab.setFedCall(-105.49);
		mab.setMarginBalance(-740.45);
		mab.setMarginBalanceWithDrawal(-2714.88);
		mab.setMarginEquity(-714.88);
		mab.setMarginableSecurities(-4183.73);
		mab.setMaxAvailableForWithDrawal(-2714.88);
		mab.setMinEquityCall(-25663.20);
		mab.setNonMarginableSecuritiesAndOptions(-2714.88);
		mab.setAccountId(id);
		
		
		CashAccountBalance cab = new CashAccountBalance();
		
		cab.setCashAvailableForInvestment(1000.23);
		cab.setCashBalance(203432.23);
		cab.setSettledCashForInvestment(12323.12);
		cab.setAccountId(id);
		
		account = new Account("briviere@gmail.com", "INDIVUAL", 
				id, 
				"MARGIN", 
				"", 
				657.37, 
				"INDIVIDUAL", 
				ab, 
				dtb, 
				mab, 
				cab);
		
		AccountConverter ac = new AccountConverter(account);
		*/
		
		Account ac = new Account();
		
		if(AccountResponseDao.instance.getModel().containsKey(accountId)) {
			ac = AccountResponseDao.instance.getModel().get(accountId);
		}
			
		return ac;
	}
	
	@PUT
	@Consumes({"application/xml","application/json"})
	public Response createNewAccount (JAXBElement <Account> account) {
		
		
		return putAndGetResponse(account.getValue());
		
    }
	
	
	
	private Response putAndGetResponse(Account accountResponse) {
		Response res;
		if(AccountResponseDao.instance.getModel().containsKey(accountResponse.getAccountId())) {
			res = Response.noContent().build();
		} else {
			res = Response.created(uriInfo.getAbsolutePath()).build();
		}
		AccountResponseDao.instance.getModel().put(accountResponse.getAccountId(), accountResponse);
		return res;
	}
}
